using System;
using UnityEngine;
using System.Collections.Generic;
using System.Threading.Tasks;

public sealed class Spawner : MonoBehaviour
{
    public MyComponent _prefab = null;

    List<MyComponent> _spawned = new List<MyComponent>();
    
    Texture2D[] _tex = new Texture2D[256];

    private bool tested = false;
    async void TestTest()
    {
        if (tested)
            return;

        tested = true;
        // Debug.Log("Test Start");
        
        for (var i = 0; i < 256; i++)
            _spawned.Add(Instantiate(_prefab));

        // await Awaitable.WaitForSecondsAsync(3);
        await Task.Delay(3000);

        foreach (var o in _spawned)
            Destroy(o.gameObject);

        // IMPORTANT : 1
        // _spawned should be cleared!
        // _spawned.Clear();
        
        // Debug.Log("Test End");
    }

    async void TestTexture()
    {
        if (tested)
            return;

        Debug.Log("Test Start");
        tested = true;
        for (var i = 0; i < 256; ++i)
        {
            _tex[i] = new Texture2D(256, 256);
        }
        
        await Task.Delay(3000);
        
        foreach (var o in _tex)
            Destroy(o);
        
        Debug.Log("Test End");
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            TestTest();
            // TestTexture();
        }
    }
}
